package com.lx.netty.sixth;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

import java.util.Random;

public class TestClientHandler extends SimpleChannelInboundHandler<MyDataInfo.MyMessage> {

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, MyDataInfo.MyMessage msg) throws Exception {


    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        int randomInt = new Random().nextInt(3);
        MyDataInfo.MyMessage.Builder messageBuilder= MyDataInfo.MyMessage.newBuilder();
        switch (randomInt){
            case 0 :
                messageBuilder.setDataType(MyDataInfo.MyMessage.DataType.PersonType)
                        .setPerson(
                                MyDataInfo.Person.newBuilder()
                                        .setAddress("北京")
                                .setAge(20).setName("张三").build()
                        );
                break;
            case 1:

                messageBuilder.setDataType(MyDataInfo.MyMessage.DataType.DoyType)
                        .setDog(
                                MyDataInfo.Dog.newBuilder()
                                        .setAge(2).setName("刀哥").build()
                        );
                break;

            case 2:
                messageBuilder.setDataType(MyDataInfo.MyMessage.DataType.CatType)
                        .setCat(
                                MyDataInfo.Cat.newBuilder()
                                        .setCity("保健乡").setName("凯特").build()
                        );
                break;
            default:
        }
        messageBuilder.build();

        MyDataInfo.Person person=  MyDataInfo.Person.newBuilder().setName("张三").setAge(20).setAddress("北京").build();
        ctx.channel().writeAndFlush(messageBuilder);
    }
}
